﻿/**
 * ...
 * @author aboutwhat
 */

package format.hxml;
import format.hxml.Reader;
import haxe.io.StringInput;
import haxe.unit.TestCase;
import format.hxml.Data.Option;

using format.hxml.Data.OptionTools;

class ReaderTest extends BaseTestCase 

{
    
    
    public function new ()
    {
        super();
    }
    
    
    
    public function testRead_Main ():Void 
    {
        var str = '
            -main Main
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        var actual = data.pop();
        var expected = Option.main("Main");
        
        
        assertTrue(actual.equals(expected));
    }

    public function testRead_Cmd () : Void {
         var str = '
            -cmd "myapp -paramA A -paramB B  "  
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        var actual = data.pop();
        var expected = Option.cmd('"myapp -paramA A -paramB B  "');
        
        
        assertTrue(actual.equals(expected));
        

    }
    
    public function testRead_SwfHeader () : Void {
        var str = '
            -swf-header 400:200:10:FF0000
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        var actual = data.pop();
        var expected = Option.swfHeader( { width : 400, height : 200, fps : 10, color : 0xFF0000 } );
        
        
        assertTrue(actual.equals(expected));
    }
    
    public function testRead_Library () : Void {
        var str = '
            -lib neash
            -lib nme:2.1
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        // first lib neash
        
        var actual = data.pop();
        var expected = Option.lib( { library : "neash", version : null } );
        
        assertTrue(actual.equals(expected));
        
        // second lib nme with version
        
        var actual = data.pop();
        var expected = Option.lib( { library : "nme", version : "2.1" } );
        
        assertTrue(actual.equals(expected));
    }
    
    public function testRead_Library_InvalidSyntax () : Void {
        var str = '
            -lib nme : 2.1
        ';
        var reader = new Reader(new StringInput(str));
        try {
            var data = reader.read();
            fail();
        } catch (e : String) {
            succeed();
        }
        
    }
    
    public function testRead_Resource () : Void {
        var str = '
            -resource myfile.xml@resource_name
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        var actual = data.pop();
        var expected = Option.resource( { file : "myfile.xml", name : "resource_name" } );
        
        assertTrue(actual.equals(expected));
        
    }
    
    public function testRead_Resource_InvalidSyntax () : Void {
        var str = '
            -resource myfile.xml @ resource_name
        ';
        var reader = new Reader(new StringInput(str));
        
        try {
            var data = reader.read();
            fail();
        } catch (e : String) {
            succeed();
        }
        
    }
    
    public function testRead_Remap () : Void {
        var str = '
            -remap neko:cpp
        ';
        var reader = new Reader(new StringInput(str));
        var data = reader.read();
        
        var actual = data.pop();
        var expected = Option.remap( { source : "neko", target : "cpp" } );
        
        assertTrue(actual.equals(expected));
        
    }
    
    public function testRead_Remap_InvalidChars () : Void {
        var str = '
            -remap 123@neko
        ';
        var reader = new Reader(new StringInput(str));
        
        try {
            var data = reader.read();
            fail();
        } catch (e : String) {
            succeed();
        }
        
    }
    
    
}